<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@page import="java.util.Collection" %>
<%@page import="java.util.Iterator" %>
<%@page import="com.liuya.safe.model.SafeRole" %>
<%@ include file="../common/taglib.jsp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%
    I18N i18n = I18N.getWebInstance(request);
%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="shortcut icon" href="${contextPath}/style/def/favicon.ico">
    <%@ include file="../common/static.jsp" %>

    <script src="../js/validate/<%=i18n.getValidateMessageFile()%>" type="text/javascript"></script>

    <title><%=i18n.say("Roles")%>
    </title>
<body>
<jsp:include page="../common/header.jsp"></jsp:include>
<%@include file="../common/menu.jsp" %>

<%
    Collection coll = (Collection) request.getAttribute("roles");
    int totalNumber = ((Integer) request.getAttribute("totalNumber")).intValue();
    int first = ((Integer) request.getAttribute("first")).intValue();
    int size = ((Integer) request.getAttribute("size")).intValue();
    String searchName = (String) request.getAttribute("name");

    if (searchName == null) {
        searchName = "";
    }
%>

<div class="smallContainer">

    <%@ include file="../common/searchForm.jsp" %>

    <label><%=i18n.say("Role_list")%>
    </label>
    <table class="ralaTable">
        <tr>
            <th><%=i18n.say("Name")%>
            </th>
            <th><%=i18n.say("Description")%>
            </th>
            <th><%=i18n.say("Action")%>
                <a href="javascript:addRole()" title="Add"><img src="${contextPath}/style/images/add.gif"/></a></th>
        </tr>
        <%
            for (Iterator iter = coll.iterator(); iter.hasNext(); ) {
                SafeRole role = (SafeRole) iter.next();
        %>
        <tr>
            <td><%=role.getName() %>
            </td>
            <td><%=role.getDescription() == null ? "" : role.getDescription() %>
            </td>
            <td>
                <a href="javascript:editRole(<%=role.getId() %>);" title="<%=i18n.say( "Edit" )%>"><img
                        src="${contextPath}/style/images/edit.gif"/></a>
                <a href="javascript:deleteRole(<%=role.getId() %>);" title="<%=i18n.say( "Delete" )%>"><img
                        src="${contextPath}/style/images/delete.gif"/></a>
                <a href="/rolePrivilege/rolePrivilege.do?roleId=<%=role.getId() %>"
                   title="<%=i18n.say( "Set_privileges" )%>"><img src="${contextPath}/style/images/privilege.png"/></a>
            </td>
        </tr>
        <% } %>
    </table>

    <form id="navForm">
        <table class="ralaTable">
            <%@include file="../common/nav.jsp" %>
        </table>
    </form>

</div>

<div id="roleDialog" title="Add/Edit role">
    <form id="roleForm" method="post">

    </form>
</div>

<jsp:include page="../common/footer.jsp"></jsp:include>

<script type="text/javascript">
    jQuery(document).ready(function () {
        var navOptions = {
            beforeSubmit: calFirst
        };

        jQuery('#navForm').ajaxForm(navOptions);

        jQuery("#roleDialog").dialog({
            autoOpen: false,
            modal: true,
            hide: "fadeOut",
            position: 'middle',
            buttons: {
                "OK": function () {
                    jQuery('#roleForm').submit();
                }
            }
        });

        var roleOptions = {
            beforeSubmit: checkRoleForm,
            success: afterUpdateRole
        };

        jQuery('#roleForm').submit(function () {
            jQuery(this).ajaxSubmit(roleOptions);
            return false;
        });
    });

    function checkRoleForm() {
        return jQuery('#roleForm').valid();
    }

    function setValidate(roleId) {
        var url = "isNameValid.do?oper=isNameValid";
        if (roleId != "__") {
            url = url + "&id=" + roleId;
        }

        //validate
        jQuery('#roleForm').validate({
            rules: {
                name: {
                    required: true,
                    remote: {
                        url: url,
                        type: "GET",
                        dataType: "json",
                        data: {
                            name: function () {
                                return jQuery("#roleForm :input[name=name]").val();
                            }
                        }
                    }
                }
            }
        });
    }

    function afterUpdateRole() {
        jQuery("#roleDialog").dialog("close");
        window.location = "roleList.do";
    }

    function calFirst(formData, jqForm, options) {
        var totalNumber =<%=totalNumber%>;
        var size =<%=size%>;

        var page2 = jQuery("#navForm :input[name=page]").val();
        var page = page2 - 1;
        if (page * size >= 0 && page * size < totalNumber) {
            window.location = "roleList.do?name=<%=searchName%>&first=" + (page * size);
            return true;
        } else {
            return false;
        }
    }

    function gotoRecord(first) {
        window.location = "roleList.do?name=<%=searchName%>&first=" + first;
    }

    function deleteRole(id) {
        jQuery.ajax({
            url: 'deleteRole.do?oper=deleteRole',
            type: 'POST',
            async: false,
            data: {"id": id},
            error: function () {
                alert('Error delete role');
            },
            success: function (xml) {
                window.location = "roleList.do?name=<%=searchName%>&first=<%=first%>";
            }
        });
    }

    function addRole() {
        $("#roleForm").attr("action", "save.do");
        jQuery('#roleForm').load("addRole.do?oper=addRole",
                function () {
                    jQuery("#roleDialog").dialog("open");
                });
    }

    function editRole(id) {
        $("#roleForm").attr("action", "updateRole.do?id=" + id);
        jQuery('#roleForm').load("getRole.do?oper=getRole&id=" + id,
                function () {
                    jQuery("#roleDialog").dialog("open");
                });
    }
</script>

</body>
</html>